package com.qumitech.visualnovel.service.message.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.message.SystemMessageConfigDTO;
import com.qumitech.visualnovel.controller.vo.message.SystemMessageVO;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.message.SystemMessageService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2019/6/21 13:46
 * @Description:
 */
@Service
public class SystemMessageServiceImpl implements SystemMessageService {
    private final String SYSTEM_MSG_URL = "/system/message";
    private final String SYSTEM_MSG_CONFIG_URL = "/system/message/config";
    private final String SYSTEM_MSG_CONFIG_ADMIN_URL = "/admin/system/message/config";

    @Override
    public void sendScheduleMsg(SystemMessageVO systemMessageVO) {
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_URL;
        systemMessageVO.setNotifyType((byte) 2);
        systemMessageVO.setTimeType((byte) 1);
        String req = JSONObject.toJSONString(systemMessageVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getScheduleMsg(Integer offset, Integer limit, Long startTime, Long endTime) {
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_URL;
        Map<String, String> paramMap = new HashMap<>();
        if (startTime != null) {
            paramMap.put("start_time", startTime.toString());
        }
        if (endTime != null) {
            paramMap.put("end_time", endTime.toString());
        }
        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        if (rspMap.get("system_msg") == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rspMap;
    }

    @Override
    public void deleteScheduleMsg(String msgId) {
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_URL;
        SystemMessageVO systemMessageVO = new SystemMessageVO();
        systemMessageVO.setMsgId(msgId);
        systemMessageVO.setDeleteFlag((byte) 1);
        String req = JSONObject.toJSONString(systemMessageVO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updateScheduleMsg(SystemMessageVO systemMessageVO) {
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_URL;
        String req = JSONObject.toJSONString(systemMessageVO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> listSystemMessageConfigDTO( Byte msgCategoryType,String operator,String configId, Byte state, String startTime, String endTime, Integer limit, Integer offset) {

        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_CONFIG_ADMIN_URL;
        Map<String, String> paramMap = new HashMap<>();

        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (state != null) {
            paramMap.put("state", state.toString());
        }
        if (startTime != null) {
            paramMap.put("startTime", startTime);
        }
        if (endTime != null) {
            paramMap.put("endTime", endTime);
        }
        if (configId != null) {
            paramMap.put("config_id", configId);
        }
        if (operator != null) {
            paramMap.put("operator", operator);
        }
        if (msgCategoryType != null) {
            paramMap.put("msg_category_type", msgCategoryType.toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addSystemMessageConfig(SystemMessageConfigDTO systemMessageConfigDTO) {
        if (systemMessageConfigDTO.getUids() != null)
        {
             String []uids = systemMessageConfigDTO.getUids().split(",");
             for(String uid: uids)
             {
                 if(!StringUtils.isNumber(uid))
                 {
                     throw new BusinessException(ResultEnum.SYSTEM_CONFIG_ADD_UID_PARAM_ERROR);
                 }
             }
        }
        systemMessageConfigDTO.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_CONFIG_URL;
        String req = JSONObject.toJSONString(systemMessageConfigDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeSystemMessageConfig(String configId) {
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_CONFIG_URL + "/" + configId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updateSystemMessageConfig(SystemMessageConfigDTO systemMessageConfigDTO) {
        systemMessageConfigDTO.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + SYSTEM_MSG_CONFIG_URL + "/" + systemMessageConfigDTO.getConfigId();
        String req = JSONObject.toJSONString(systemMessageConfigDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
